package com.basker.pisces.data.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.basker.pisces.orm.database.BeanMetaDatabaseSynchronizer;
import com.basker.pisces.orm.parser.MySqlParser;
import com.basker.pisces.orm.repository.DefaultRepository;
import com.basker.pisces.orm.session.DefaultSqlSessionFactory;

/**
 * 数据访问相关的bean配置
 *
 * @author hangwen
 */
@Configuration
@ConditionalOnClass(DefaultRepository.class)
public class DataRepositoryConfiguration {

    @Bean
    @ConditionalOnProperty(name = "pisces.orm.db.syn.enable", matchIfMissing = false)
    public BeanMetaDatabaseSynchronizer beanMetaDatabaseSynchronizer() {
        return new BeanMetaDatabaseSynchronizer();
    }

    @Bean
    @ConditionalOnMissingBean(DefaultRepository.class)
    public DefaultRepository defaultRepository() {
        return new DefaultRepository();
    }

    // @Bean
    // @ConditionalOnMissingBean(DefaultSqlSession.class)
    // @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    // public DefaultSqlSession defaultSqlSession() {
    // return new DefaultSqlSession();
    // }

    @Bean
    public DefaultSqlSessionFactory defaultSqlSessionFactory() {
        return new DefaultSqlSessionFactory();
    }

    @Bean
    public MySqlParser mySqlParser() {
        return new MySqlParser();
    }
}
